package com.wx.service.impl;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wx.dao.SysUserSecurityMapper;
import com.wx.entity.SysUserSecurity;
import com.wx.entity.SysUserSecurityExample;
import com.wx.service.SysUserSecurityService;
import com.zyht.common.BaseDao;
import com.zyht.common.BaseServiceImpl;
import com.zyht.common.util.NCalendarUtil;
import com.zyht.website.entity.SysUser;
import com.zyht.website.service.SysUserService;
import com.zyht.website.shiro.UserEnum;
@Service
public class SysUserSecurityServiceImpl extends BaseServiceImpl<SysUserSecurity, SysUserSecurityExample> implements SysUserSecurityService{

	@Resource
	private SysUserSecurityMapper sysUserSecurityMapper;
	@Resource
	private SysUserService sysUserService;
	
	@Override
	public BaseDao<SysUserSecurity, SysUserSecurityExample> dao() {
		return sysUserSecurityMapper;
	}

	@Override
	@Transactional(readOnly=false)
	public int checkErrorTimes(Integer userid) {
		if(userid==null){
			return -1;
		}
		SysUserSecurityExample example = new SysUserSecurityExample();
		example.createCriteria().andUserIdEqualTo(userid);
		List<SysUserSecurity> list = this.list(example);
		//没有数据 - 新增一个
		if(list.isEmpty()){
			SysUserSecurity  sce = new SysUserSecurity();
			sce.setUserId(userid);
			sce.setErrorTimes(1);
			sce.setErrorDate(new Date());
			this.addOne(sce);
			return 4;
		}
		//没有错误时间
		SysUserSecurity security = list.get(0);
		Date errorDate = security.getErrorDate();
		if(errorDate==null){
			security.setErrorDate(new Date());
			security.setErrorTimes(1);
			this.updateById(security);
			return 4;
		}
		//错误时间为今天以前
		Date integralTime = NCalendarUtil.localDate2Date(LocalDate.now());
		if(integralTime.after(errorDate)){
			security.setErrorDate(new Date());
			security.setErrorTimes(1);
			this.updateById(security);
			return 4;
		}
		//今天的记录为空
		Integer errorTimes = security.getErrorTimes();
		if(errorTimes==null){
			security.setErrorTimes(1);
			this.updateById(security);
			return 4;
		}
		Integer totalNum = UserEnum.PWD_ERROR_TIMES.getInteger();
		int nowNum = totalNum-errorTimes;
		if(nowNum <= 0){
			security.setErrorTimes(totalNum);
			this.updateById(security);
			SysUser user = sysUserService.selectByPrimaryKey(userid);
			user.setStatus(UserEnum.STATUS_DISABLE.getValue());
			sysUserService.updateByPrimaryKey(user);
			return 0;
		}
		errorTimes += 1;
		security.setErrorTimes(errorTimes);
		this.updateById(security);
		return nowNum;
	}

	@Override
	@Transactional(readOnly=false)
	public boolean checkChangePwd(Integer userid) {
		//当前时间 - 修改时间  > 120天 就修改密码
		SysUserSecurityExample example = new SysUserSecurityExample();
		example.createCriteria().andUserIdEqualTo(userid);
		List<SysUserSecurity> list = this.list(example);
		//没有数据 - 新增一个
		if(list.isEmpty()){
			SysUserSecurity  sce = new SysUserSecurity();
			sce.setUserId(userid);
			sce.setChangeTime(new Date());
			this.addOne(sce);
			return false;
		}
		SysUserSecurity security = list.get(0);
		Date changeTime = security.getChangeTime();
		//时间为空 更新时间
		if(changeTime==null){
			security.setChangeTime(new Date());
			this.updateById(security);
			return false;
		}
		//密码有效期
		Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(changeTime);
        rightNow.add(Calendar.DAY_OF_YEAR,UserEnum.PWD_INTERVAL.getInteger());
        Date validity =rightNow.getTime();
		//有效期 > 当前时间 = 不用修改
        Date now = new Date();
        if(validity.after(now)){
        	return false;
        }
		return true;
	}

	@Override
	public void updatePwdChangeTime(Integer userid, Date date) {
		SysUserSecurityExample example = new SysUserSecurityExample();
		example.createCriteria().andUserIdEqualTo(userid);
		List<SysUserSecurity> list = this.list(example);
		if(!list.isEmpty()){
			SysUserSecurity security = list.get(0);
			security.setChangeTime(date);
			this.updateById(security);
		}
	}

	@Override
	public void updateErrorTimes(Integer userid,Integer num) {
		SysUserSecurityExample example = new SysUserSecurityExample();
		example.createCriteria().andUserIdEqualTo(userid);
		List<SysUserSecurity> list = this.list(example);
		if(!list.isEmpty()){
			SysUserSecurity security = list.get(0);
			security.setErrorTimes(num);
			this.updateById(security);
		}
	}

}
